package com.itany.book.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.constant.Constant;
import com.itany.exception.AlreadyExaminedException;
import com.itany.exception.RequestParamsException;
import com.itany.mapper.BookMapper;
import com.itany.mapper.BookSupplierMapper;
import com.itany.mapper.ExamineMapper;
import com.itany.pojo.Book;
import com.itany.pojo.BookSupplier;
import com.itany.pojo.Examine;
import com.itany.pojo.ManagerUser;
import com.itany.service.ExamineService;
import com.itany.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class ExamineServiceImpl implements ExamineService {

    @Autowired
    ExamineMapper examineMapper;

    @Autowired
    BookMapper bookMapper;

    @Autowired
    BookSupplierMapper bookSupplierMapper;

    /**
     * 查找所有审核条目
     *
     * @param page 页数
     * @param rows 每页数据条数
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public PageInfo<Examine> findAll(Integer page, Integer rows, String booknameKey, String ISBNKey,
                                     String supplierId, HttpSession session) throws RequestParamsException {
        Integer supplierIdInt = null;
        Object login = session.getAttribute("login");
        if (login instanceof ManagerUser) {
            ManagerUser managerUser = (ManagerUser) login;
            if (managerUser.getSupplier() != null) {
                supplierIdInt = managerUser.getSupplier().getId();
            }
        }
        if (!StrUtil.isEmptyIfStr(supplierId)) {
            supplierIdInt = Integer.parseInt(supplierId);
        }
        PageHelper.startPage(page, rows);
        List<Examine> list;
        list = examineMapper.selectAll(StringUtils.escapeStr(booknameKey), StringUtils.escapeStr(ISBNKey),
                supplierIdInt);
        return new PageInfo<>(list);
    }

    /**
     * 审核图书信息
     *
     * @param examineStatus 审核结果
     * @param examineDetail 审核备注
     */
    @Override
    public void examineBook(String examineStatus, String examineDetail, String examineId) throws RequestParamsException, AlreadyExaminedException {
        Integer id;
        Integer flag;
        try {
            flag = Integer.parseInt(examineStatus);
            id = Integer.parseInt(examineId);
        } catch (NumberFormatException e) {
            throw new RequestParamsException("请求参数格式错误");
        }
        Examine examine = examineMapper.selectById(id);
        if (!Constant.UNDER_REVIEW.equals(examine.getFlag())) {
            throw new AlreadyExaminedException("已经审核过，无需再次审核");
        }
        String title = "《" + examine.getBook().getBookname() + "》审核完成";
        examineMapper.updateExamine(flag, examineDetail, title, id);
        //如果书籍状态为审核态，则更改为审核相同状态，否则，不更改书籍状态
        if (Constant.UNDER_REVIEW.equals(examine.getBook().getFlag())) {
            bookMapper.updateStatus(flag, examine.getBook().getId());
        }
//        如果审核成功，将供应商提供的书籍数量添加到book_supplier中，作为售卖使用
// 将供应商提供的该书的数量累加到该书库存量中
        if (Constant.REVIEW_PASSED.equals(flag)) {
            Book aBook = bookMapper.selectById(examine.getBook().getId());
            aBook.setNumber(aBook.getNumber() + examine.getNumber());
            bookMapper.updateBook(aBook);
            BookSupplier bookSupplier = new BookSupplier();
            bookSupplier.setBookid(examine.getBook().getId());
            bookSupplier.setSupplierid(examine.getSupplier().getId());
            BookSupplier aBookSupplier = bookSupplierMapper.selectByBookAndSupplier(bookSupplier);
            if (aBookSupplier == null) {
                bookSupplier.setSellnum(Constant.INIT_BOOK_SALE_NUMBER);
                bookSupplier.setNumber(examine.getNumber());
                bookSupplierMapper.insertBookSupplier(bookSupplier);
            } else {
                aBookSupplier.setNumber(aBookSupplier.getNumber() + examine.getNumber());
                bookSupplierMapper.updateBookSupplier(aBookSupplier);
            }
        }
    }

}
